var common = require('../../utils/common.js');
var ajax = require('../../utils/ajax.js');
var WxParse = require('../../utils/wxParse/wxParse.js');
var util = require('../../utils/util.js');
var app = getApp();

Page({

	data: {
		dataLoaded: false,
		isCollected: false
	},

	onShareAppMessage: function() {
		return {
			title: this.data.title,
			path: "pages/article/article?artId=" + this._artId + '&recommend=' + app.getUserId()
		}
	},

	onLoad(options) {
		var _self = this;

		if (options.recommend) {
			app.setRecommend(options.recommend);
		}

		common.initApp({
			extSuc: function() {
				_self._artId = options.artId;
				_self._getArticel(options.artId);
			},
			loginSuc: function() {
				_self.$loginSuc();
			}
		});

	},

	_getArticel(artId) {
		var _self = this;

		ajax.G('/openapi/CHome/article_detail', {
			artId: artId,
		}, function(data) {
			var d = data['result'];

			if (d.content) {
				WxParse.wxParse('artContent', 'html', d.content, _self, 0);
			}

			_self.setData({
				dataLoaded: true,
				title: d.title,
				date: d.create_time,
				isCollected: d.isCollect,
				collectId: d.collectId,
			});
		});
	},

	//收藏
	_collect(evt) {
		var _self = this;
		var formId = evt.detail.formId;

		ajax.P('/openapi/CUser/doCollect', {
			id: _self._artId,
			type: 1
		}, function(data) {

			_self.setData({
				isCollected: true,
				collectId: data.result.id
			});
			util.showOK('收藏成功');

		});
		common.submitForm(formId);
	},

	//取消收藏
	_delCollect(evt) {
		var _self = this;
		var collectId = _self.data.collectId;
		var formId = evt.detail.formId;

		ajax.P('/openapi/CUser/delCollections', {
			idStr: collectId + ''
		}, function(data) {

			_self.setData({
				isCollected: false
			});
			util.showOK('已取消收藏');

		});
		common.submitForm(formId);
	}

});